<?php 
/**
 * 宴会订单管理
 * @author chaokai@gz-zc.cn
 */
class Dinner extends MY_Controller{
    
    public function __construct(){
        
        parent::__construct();
        $this->load->model(array(
                        'Model_dinner' => 'Mdinner',
                        'Model_dish' => 'Mdish',
                        'Model_combo' => 'Mcombo',
                        'Model_customer' => 'Mcustomer',
                        'Model_user' => 'Muser',
                        'Model_user_dinner' => 'Muser_dinner',
                        'Model_dinner_venue' => 'Mdinner_venue',
                        'Model_dinner_album' => 'Mdinneralbum',
                        'Model_dinner_images' => 'Mdinnerimages',
                        'Model_admins' => 'Madmins'
        ));
        
        $this->load->library('form_validation');
    }
    
    /**
     * 年月列表
     * @author chaokai@gz-zc.cn
     */
    public function index(){
        
        $data = $this->data;
        $data['title'] = array('首页', '订单管理');
        $year = date('Y');
        $data['year'] = array($year, $year+1, $year+2);
        
        //获取每个月的订单数量
        $data['orders'] = $this->Mdinner->get_count();
        $this->load->view('dinner/index', $data);
    }
    
    /**
     * 某月订单列表
     * @author chaokai@gz-zc.cn
     */
    public function lists(){
        $data = $this->data;
        $data['title'] = array('首页', '订单管理', '订单列表');
        $year = intval($this->input->get('year'));
        $month = intval($this->input->get('month'));
        
        $name = trim($this->input->get('name'));
        $mobile = trim($this->input->get('mobile_phone'));
        if($name || $mobile){
            $data['list'] = $this->Mdinner->search_dinner_list($name, $mobile);
        }else{
            $data['year'] = $year = intval($year);
            $data['month'] = $month = intval($month);
            !$year && show_404();
            (!$month || $month > 12) && show_404();
            
            //获取订单
            $data['list'] = $this->Mdinner->get_dinner_list($year, $month);
            //处理订单列表，未预定天显示为空
            #删除空白天处理
//             $data['list'] = $this->deal_list($data['list'], $month, $year);
        }
        
        if($data['list']){
            $create_admins = array_column($data['list'] , 'create_admin', 'id');
            $create_admins = $this->Madmins->get_lists('id, fullname',array('in' => array('id' => $create_admins)));
            $create_admins = array_column($create_admins , 'fullname', 'id');
            
            foreach ($data['list'] as $key => $val){
                if(isset($data['list'][$key]['create_admin'])){
                    $data['list'][$key]['create_admin'] =$create_admins[$val['create_admin']];
                }
            }
        }
        $this->load->view('dinner/lists', $data);
    }
    
    /**
     * 添加预约
     * @author chaokai@gz-zc.cn
     */
    public function add(){
        $data = $this->data;
        //获取所有场馆名字
        $venue_list = $this->Mvenue->get_lists('id, name, cover_img', array('is_del'=>0));
        $venue_name = array_column($venue_list, 'name', 'id');
        if(IS_POST){
            #数据验证
            $this->form_validation->set_rules('name', '姓名', 'trim|required', array('required' => '%s不能为空'));
            $this->form_validation->set_rules('venue_id[]', '场馆', 'trim|required', array('required' => '%s不能为空'));
            $this->form_validation->set_rules('venue_type', '宴会类型', 'trim|required|integer', array('required' => '%s不能为空', 'integer' => '数值类型不合法'));
            $this->form_validation->set_rules('solar_time', '公历时间', 'trim|required', array('required' => '%s不能为空'));
    
            if($this->form_validation->run() == false){
                $this->return_failed(validation_errors());
            }
            $post_data = $this->input->post();
            //查询当天有哪些厅已经预定
            if($post_data['solar_time']){
                $venue_list = $this->Mdinner->get_lists('id,solar_time', array('solar_time'=>$post_data['solar_time'], 'is_del' => 0));
                //获取订单id
                $data['dinner_id'] = array_column($venue_list, 'id');
                if($data['dinner_id']){
                    $query['in'] = array('dinner_id'=>$data['dinner_id']);
                    $venue_list = $this->Mdinner_venue->get_lists('venue_id, dinner_id', $query);
                    $venue_id = array_column($venue_list, 'venue_id');
                    foreach ($post_data['venue_id'] as $k=>$v){
                        if(in_array($v, $venue_id)){
                            $this->return_failed($venue_name[$v].'已经被预约过了!');
                        }
                    }
                }
            }
            
            
            //验证手机号
            if(!preg_match(C('regular_expression.mobile'), $post_data['mobile_phone'])){
                $this->return_failed('手机号格式不正确');
            }
            //判断该天是否被预约过
            $appoint_list = $this->appoint_exist($post_data['venue_id'], $post_data['solar_time']);
            if($appoint_list['id'] == 1){
                $this->return_failed('以下场馆已被预约<br>'.$appoint_list['name']);
            }
            #判断客户是否存在
            $user_exist = $this->Mcustomer->get_one('id', array('mobile_phone' => $post_data['mobile_phone'], 'name' => $post_data['name']));
            $user_data = array(
                            'name' => $post_data['name'],
            );
            if($user_exist){
                $customer_id = $user_exist['id'];
                $this->Mcustomer->update_info(array('is_order_customer' => 1), array('id' => $user_exist['id']));
            }else{
                $user_data['mobile_phone'] = $post_data['mobile_phone'];
                $user_data['is_order_customer'] = 1;
                $user_data['create_admin'] = $this->data['userInfo']['id'];
                $user_data['create_time'] = $user_data['update_time'] = date('Y-m-d H:i:s');
                $customer_id = $this->Mcustomer->create($user_data);
            }
            $post_data['user_id'] = $customer_id;
    
            if($post_data['venue_type'] != C('party.wedding.id')){
                $post_data['roles_main'] = $post_data['roles_main_other'];
                $post_data['roles_main_mobile'] = $post_data['roles_main_other_mobile'];
                unset($post_data['roles_wife'], $post_data['roles_wife_mobile']);
            }
            unset($post_data['roles_main_other'], $post_data['roles_main_other_mobile']);
            
            $post_data['create_time'] = $post_data['update_time'] = date('Y-m-d H:i:s');
            $post_data['create_admin'] = $post_data['update_admin'] = $data['userInfo']['id'];
            
            #开启事务
            $this->db->trans_start();
    
            $user = $post_data;
            unset($post_data['mobile_phone'], $post_data['name'], $post_data['id_number']);
    
            //宴会菜单详情
            $menus = $post_data['menus'];
            unset($post_data['menus']);
            //场馆ids
            $venue_ids = $post_data['venue_id'];
            unset($post_data['venue_id']);
            //获取阴历日期
            $lunar_time = solar_to_lunar($post_data['solar_time']);
            $post_data['lunar_time'] = $lunar_time['lunar_time'];
            //保存订单
            //处理pc端封面图片
            if(isset($post_data['cover_img'])){
                $post_data['cover_img'] = implode(';', $post_data['cover_img']);
            }
            //处理手机端封面图片
            if(isset($post_data['m_cover_img'])){
                $post_data['m_cover_img'] = implode(';', $post_data['m_cover_img']);
            }
            $dinner_id = $this->Mdinner->create($post_data);
            !$dinner_id && $this->return_failed('操作失败');
            
            //保存客户信息；生成手机号前台登录账号
            $customer_id = $this->create_user($user, $customer_id, $dinner_id);
            //保存信息到客户场馆对应表
            $dinner_venues = array();
            foreach ($venue_ids as $k => $v){
                $dinner_venues[] = array(
                                'dinner_id' => $dinner_id,
                                'venue_id' => $v
                );
            }
            $this->Mdinner_venue->create_batch($dinner_venues);
            
            //保存菜单信息
            if($menus){
                $this->add_appoint_detail($dinner_id, $menus);
            }
            
            $this->db->trans_complete();
            
            if($this->db->trans_status() === false){
                $this->return_failed('保存失败');
            }else{
                $this->return_success([], '保存成功');
            }
    
        }
        
        $data['year'] = $this->input->get('year');
        $data['month'] = $this->input->get('month');
        $data['day'] = $this->input->get('day');
        $data['venue_id'] = $this->input->get('venue_id');
        
        if($data['year'] && $data['month']){
            $month = $data['month'] < 10 ? '0'.$data['month'] : $data['month'];
            $this->load->helper('date');
            $days = days_in_month($data['month'], $data['year']);
            $data['min_date'] = $data['year'].'-'.$month.'-01';
            $data['max_date'] = $data['year'].'-'.$month.'-'.$days;
        }
    
        $data['title'] = array('订单列表', '添加预约');
        //宴会类型
        $data['party_type'] = C('party');
        //场馆列表
        $data['venue_list'] = $this->Mvenue->lists();
        //套餐菜品
        $data['combo_menu'] = $this->Mcombo->lists();
        $this->load->view('dinner/add', $data);
    }
    
    /**
     * 宴会相册
     * @author yonghua
     */
    public function album(){
        $data = $this->data;
        $dinner_id = (int) $this->input->get('dinner_id');
        if($dinner_id <= 0){
            $this->error('非法访问参数');
        }
        $data['dinner_id'] = $dinner_id;
        $data['list'] = $this->Mdinneralbum->get_lists('*', ['dinner_id' => $dinner_id, 'is_del' => 0]);
        $this->load->view('dinner/album', $data);
    }
    
    /**
     * 宴会相册详情
     * @author yonghua
     */
    public function album_detail(){
        $data = $this->data;
        $pageconfig = C('page.config_log');
        $this->load->library('pagination');
        $page = (int)$this->input->get_post('per_page') ? : '1';
        $data['page'] = $page;
        $id = (int) $this->input->get('album_id');
        $data['dinner_id'] = (int) $this->input->get('dinner_id');
        $size = 10;
        if($id == 0){
            $this->error('非法访问参数');
        }
        $where = ['is_del' => 0, 'album_id' => $id];
        $list = $this->Mdinnerimages->get_lists('id,img,thumb,sy_img',$where, ['create_time'], $pageconfig['per_page'], ($page-1)*$pageconfig['per_page']);
        if($list){
            $data['list'] = $list;
            // 分页信息
            $data['count'] = $this->Mdinnerimages->count($where);
            $where['dinner_id'] = $data['dinner_id'];
            $pageconfig['base_url'] = "/dinner/album_detail?".http_build_query($where);
            $pageconfig['total_rows'] = $data['count'];
            $this->pagination->initialize($pageconfig);
            $data['pagestr'] = $this->pagination->create_links();
        }
        $this->load->view('dinner/album_detail', $data);
    }
    
    /**
     * 添加宴会相册
     * @author yonghua
     */
    public function album_add(){
        $data = $this->data;
        $dinner_id = (int) $this->input->get('dinner_id', TRUE);
        if($dinner_id <= 0){
            $this->error('非法访问参数');
        }
        $data['dinner_id'] = $dinner_id;
        if(IS_POST){
            if(!isset($data['userInfo'])){
                $this->error('拒绝操作');
            }
            $add = $this->input->post();
            if(!isset($add['dinner_id'])){
                $this->error('非法访问参数');
            }
            if(empty(trim($add['name']))){
                $this->error('相册名称不能为空');
            }
            if((trim($add['price']) < 0)){
                $this->error('价格不能小于0');
            }
            $add['create_time'] = date('Y-m-d H:i:s');
            $add['create_admin'] = $data['userInfo']['id'];
            $res = $this->Mdinneralbum->create($add);
            if(!$res){
                $this->error('操作失败');
            }
            $this->success('添加成功', '/dinner/album?dinner_id='.$add['dinner_id']);
        }
        $this->load->view('dinner/album_add', $data);
    }
    
    /**
     * 编辑宴会相册
     * @author yonghua
     */
    public function edit_album(){
        $data = $this->data;
        if(IS_POST){
            if(!isset($data['userInfo'])){
                $this->error('拒绝操作');
            }
            $up = $this->input->post();
            $id = $up['id'];
            unset($up['id']);
            if(!isset($up['dinner_id'])){
                $this->error('非法访问参数');
            }
            if(empty(trim($up['name']))){
                $this->error('相册名称不能为空');
            }
            if(empty(trim($up['cover_img']))){
                $this->error('请上传封面图');
            }
            if((trim($up['price']) < 0)){
                $this->error('价格不能小于0');
            }
            $res = $this->Mdinneralbum->update_info($up, ['id' => $id]);
            if(!$res){
                $this->error('操作失败');
            }
            $this->success('修改成功', '/dinner/album?dinner_id='.$up['dinner_id']);
        }
        $id = (int) $this->input->get('id');
        $data['dinner_id'] = (int) $this->input->get('dinner_id');
        if($id == 0){
            $this->error('非法访问参数');
        }
        $info = $this->Mdinneralbum->get_one('*', ['id' => $id]);
        if(!$info){
            $this->error('相册不存在');
        }
        $data['info'] = $info;
        $this->load->view('dinner/edit_album', $data);
    }
    
    /**
     * 删除宴会相册
     * @author yonghua
     */
    public function del_album(){
        $data = $this->data;
        if(!isset($data['userInfo'])){
            $this->error('系统拒绝操作');
        }
        $id = (int) $this->input->get('id');
        $dinner_id = (int) $this->input->get('dinner_id');
        if( ($id == 0) && ($dinner_id == 0)){
            $this->error('非法访问参数');
        }
        $count = $this->Mdinnerimages->count(['album_id' => $id, 'is_del' =>0]);
        if($count > 0){
            $this->error('该相册还有相片，删除前请先清空相册内的相片');
        }
        $res = $this->Mdinneralbum->update_info(['is_del' => 1], ['id' => $id]);
        if(!$res){
            $this->error('删除失败');
        }
        $this->success('操作成功', '/dinner/album?dinner_id='.$dinner_id);
    }
    
    
    /**
     * 保存宴会菜单详情
     * @param unknown $appoint_id
     * @param unknown $menus
     */
    private function add_appoint_detail($dinner_id, $menu){
        //查询菜品名称及图片
        $menu_detail = $this->Mcombo->get_one('id,combo_name,cover_img,description,relevance_id', array('id' => $menu));
        
        if($menu_detail){
            $menus_list['dinner_id'] = $dinner_id;
            $menus_list['menus_id'] = $menu;
            $menus_list['name'] = $menu_detail['combo_name'];
            $menus_list['cover_img'] = $menu_detail['cover_img'];
            $menus_list['dishs_id'] = $menu_detail['relevance_id'];
            $menus_list['description'] = $menu_detail['description'];
            $menus_list['create_time'] = date('Y-m-d H:i:s');
            
            if($this->Mdinner_detail->create($menus_list)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    
    }
    
    /**
     * 修改预约信息
     * @author chaokai@gz-zc.cn
     */
    public function edit($id = 0){
        $data = $this->data;
        //获取所有场馆名字
        $venue_list = $this->Mvenue->get_lists('id, name, cover_img', array('is_del'=>0));
        $venue_name = array_column($venue_list, 'name', 'id');
        if(IS_POST){
            $this->form_validation->set_rules('name', '姓名', 'trim|required', array('required' => '%s不能为空'));
            $this->form_validation->set_rules('venue_type', '宴会类型', 'trim|required|integer', array('required' => '%s不能为空', 'integer' => '数值类型不合法'));
            $this->form_validation->set_rules('roles_main', '宴会主角', 'trim|required', array('required' => '%s不能为空'));
            $this->form_validation->set_rules('solar_time', '公历时间', 'trim|required', array('required' => '%s不能为空'));
            
            if($this->form_validation->run() == false){
                $this->return_failed(validation_errors());
            }
            $post_data = $this->input->post();

            //查询当天有哪些厅已经预定
            if($post_data['solar_time']){
                $venue_list = $this->Mdinner->get_lists('id,solar_time', array('solar_time'=>$post_data['solar_time'], 'id!='=>$post_data['id'], 'is_del' => 0));
                
                //获取订单id
                $data['dinner_id'] = array_column($venue_list, 'id');
               
                if($data['dinner_id']){
                    $query['in'] = array('dinner_id'=>$data['dinner_id']);
                    $venue_list = $this->Mdinner_venue->get_lists('venue_id, dinner_id', $query);
                    
                    $venue_id = array_column($venue_list, 'venue_id');
                    foreach ($post_data['venue_id'] as $k=>$v){
                        if(in_array($v, $venue_id)){
                            $this->return_failed($venue_name[$v].'已经被预约过了!');
                        }
                    }
                }
            }
            
            //验证手机号
            if(!preg_match(C('regular_expression.mobile'), $post_data['mobile_phone'])){
                $this->return_failed('手机号格式不正确');
            }
    
            //处理封面图片
            if(isset($post_data['cover_img'])){
                $post_data['cover_img'] = implode(';', $post_data['cover_img']);
            }
            //处理封面图片
            if(isset($post_data['m_cover_img'])){
                $post_data['m_cover_img'] = implode(';', $post_data['m_cover_img']);
            }
            //修改保存用户及预约信息
            if($this->change_info($post_data)){
                $this->return_success([], '修改成功');
            }else{
                $this->return_failed('修改失败');
            }
    
            
        }
    
        $data['info'] = $this->Mdinner->info($id);
        $week = array('日', '一', '二', '三', '四', '五', '六');
        $data['info']['week'] =  '星期'. $week[date('w', strtotime($data['info']['solar_time']))];       
        $data['title'] = array('订单管理', '修改订单信息');
        //宴会类型
        $data['party_type'] = C('party');
        //场馆列表
        $data['venue_list'] = $this->Mvenue->lists();
        //套餐菜品
        $data['combo_menu'] = $this->Mcombo->lists();

        $this->load->view('dinner/edit', $data);
    }
    
    /**
     * 删除预约
     * @author chaokai@gz-zc.cn
     */
    public function del($id){
        $id = intval($id);
        !$id && $this->return_failed('参数不合法');
    
        if($this->Mdinner->update_info(array('is_del' => 1), array('id' => $id))){
            $this->return_success([], '删除成功');
        }else{
            $this->return_failed('操作失败');
        }
    }
    
    /**
     * 显示订单详情
     * @author chaokai@gz-zc.cn
     */
    public function show_detail($id = 0){
        $id = intval($id);
        !$id && show_404();
        
        $info = $this->Mdinner->info($id);
        !$info && show_404();
        //场馆
        $venue = $this->Mvenue->get_lists('name', array('in' => array('id' => $info['venue_ids'])));
        $venue_name = '';
        foreach ($venue as $v){
            $venue_name .= $v['name'].';';
        }
        $info['venue_name'] = $venue_name;
        //宴会类型
        $dinnertype = array_column(C('party'), 'name', 'id');
        $info['venue_type_name'] = $dinnertype[$info['venue_type']];
        
        $data = $this->data;
        $data['info'] = $info;
        $admin_name = $this->Madmins->get_one('id, fullname',array('id' => $data['info']['create_admin']));
        $data['info']['create_admin'] = $admin_name['fullname'];
        
        $data['title'] = array(
                        ['url' => '/common', 'text' => '首页'],
                        ['url' => $_SERVER['HTTP_REFERER'], 'text' => '订单列表'],
                        ['url' => '', 'text' => '预约详情']
        );
        
        $this->load->view('dinner/detail', $data);
    }
    
    /**
     * 上传附件
     * @author chaokai@gz-zc.cn
     */
    public function add_file($id = 0, $album_id=0){
        $data = $this->data;
        if(IS_POST){
            $post_data = $this->input->post();
            if(isset($post_data['images'])){
                $add = [];
                foreach ($post_data['images'] as $k => $v){
                     $add[$k]['album_id'] = $post_data['album_id'];
                     $add[$k]['create_admin'] = $data['userInfo']['id'];
                     $add[$k]['create_time'] = date('Y-m-d H:i:s');
                     $add[$k]['img'] = $v;
                     $add[$k]['sy_img'] = $post_data['sy_images'][$k];
                     $add[$k]['thumb'] = $post_data['ys_images'][$k];
                }
            }else{
                $this->return_failed('请先添加照片');
            }
            if($this->Mdinnerimages->create_batch($add)){
                $this->return_success([], '保存成功');
            }else{
                $this->return_failed('保存失败');
            }
        }
        
        $id = intval($id);
        !$id && show_404();
        
        $data = $this->data;
        $data['title'] = array(
                        ['url' => '/common', 'text' => '首页'],
                        ['url' => $_SERVER['HTTP_REFERER'], 'text' => '订单列表'],
                        ['url' => '', 'text' => '添加附件']
        );
        $data['title'] = array(
            ['url' => '/common', 'text' => '首页'],
            ['url' => $_SERVER['HTTP_REFERER'], 'text' => '订单列表'],
            ['url' => '', 'text' => '添加附件']
        );
        //查询存在的信息
        $info = $this->Mdinneralbum->get_lists('id,name', ['dinner_id' => $id, 'is_del' => 0]);
        if(!$info){
            $this->success('当前没有相册，请先新建一个相册', '/dinner/album?dinner_id='.$id);
        }
        if(!empty($album_id)){
            $data['album_id'] = $album_id;
        }
        $data['list'] = $info;
        $this->load->view('dinner/add_file', $data);
    }
    
    /**
     * 添加视频
     * @author chaokai@gz-zc.cn
     */
    public function add_video($id = 0){
        
        if(IS_POST){
            $post_data = $this->input->post();
        
            $where = array('id' => $post_data['id']);
            unset($post_data['id']);
            if($this->Mdinner->update_info($post_data, $where)){
                $this->return_success([], '保存成功');
        
            }else{
                $this->return_failed('保存失败');
            }
        }
        
        $id = intval($id);
        !$id && show_404();
        
        $data = $this->data;
        $data['title'] = array(
                        ['url' => '/common', 'text' => '首页'],
                        ['url' => $_SERVER['HTTP_REFERER'], 'text' => '订单列表'],
                        ['url' => '', 'text' => '添加附件']
        );
        //查询存在的信息
        $field = 'id,video,video_title,video_intro';
        $where = array('id' => $id);
        $info = $this->Mdinner->get_one($field, $where);
        $data['info'] = $info;
        
        $this->load->view('dinner/add_video', $data);
    }
    
    /**
     * 置顶显示
     * @author chaokai@gz-zc.cn
     */
    public function up_show(){
        $id = intval($this->input->post('id'));
        !$id && $this->return_failed();
        
        $max_id = $this->Mdinner->get_one('max(`order`) as order_max', array());
        $max_id = $max_id['order_max'] + 1;
        if($this->Mdinner->update_info(array('order' => $max_id), array('id' => $id))){
            $this->return_success();
        }else{
            $this->return_failed();
        }
    }
    
    /**
     * 修改预约信息及用户信息
     * @param $post_data 输入数据
     * @author chaokai@gz-zc.cn
     */
    private function change_info($post_data){
    
        $user_id = $post_data['user_id'];
        $id = $post_data['id'];
    
        $user_data = array(
                        'name' => $post_data['name'],
                        'mobile_phone' => $post_data['mobile_phone'],
        );
        //开启事务
        $this->db->trans_start();
        $this->Mcustomer->update_info($user_data, array('id' => $user_id));
        unset($post_data['user_id'], $post_data['id'], $post_data['name'], $post_data['mobile_phone'], $post_data['id_number']);
    
        $menus_detail = $post_data['menus'];
        unset($post_data['menus']);
        //删除原来的菜品详情数据
        $this->Mdinner_detail->update_info(array('is_del' => 1), array('dinner_id' => $id));
        //添加新菜品
        $this->add_appoint_detail($id, $menus_detail);
    
        if($post_data['venue_type'] != C('party.wedding.id')){
            $post_data['roles_main'] = $post_data['roles_main_other'];
            $post_data['roles_main_mobile'] = $post_data['roles_main_other_mobile'];
            unset($post_data['roles_wife'], $post_data['roles_wife_mobile']);
        }
        unset($post_data['roles_main_other'], $post_data['roles_main_other_mobile']);
        
        //删除原来宴会场馆对应表中的记录
        $this->Mdinner_venue->delete(array('dinner_id' => $id ));
        //修改场馆信息到宴会场馆对应表
        $venue_ids = $post_data['venue_id'];
        unset($post_data['venue_id']);
        $dinner_venues = array();
        foreach ($venue_ids as $k => $v){
            $dinner_venues[] = array(
                            'dinner_id' => $id,
                            'venue_id' => $v
            );
        }
        $this->Mdinner_venue->create_batch($dinner_venues);
        
        //计算阴历日期
        $lunar_time = solar_to_lunar($post_data['solar_time']);
        $post_data['lunar_time'] = $lunar_time['lunar_time'];
        //更新宴会表
        $this->Mdinner->update_info($post_data, array('id' => $id));
        $this->db->trans_complete();
        return true;
    }
    
    /**
     * 创建预订人、新郎、新娘前台登录账号
     * 判断用户账号表是否存在手机号，不存在创建账号
     * @author chaokai@gz-zc.cn
     */
    private function create_user($post_data, $customer_id, $dinner_id){
        
        $user_data = array();
        $customer_data = array(
                        'mobile_phone' => $post_data['mobile_phone'],
                        'realname' => $post_data['name'],
                        'password' => get_encode_pwd(substr($post_data['mobile_phone'], -6, 6))//取手机号后6位为登录密码
        );
        //判断是否存在预订人的手机号账户
        $customer_exist = $this->Muser->get_one('id', array('mobile_phone' => $post_data['mobile_phone']));
        if($customer_exist){
            $customer_user_id = $customer_exist['id'];
        }else{
            $customer_user_id = $this->Muser->create($customer_data);
        }
        //将预订人账号id保存到客户表
        $this->Mcustomer->update_info(array('user_id' => $customer_user_id), array('id' => $customer_id));
        $customer_data['user_id'] = $customer_user_id;
        $user_data[] = array(
                        'mobile_phone' => $post_data['roles_main_mobile'],
                        'realname' => $post_data['roles_main'],
                        'password' => get_encode_pwd(substr($post_data['roles_main_mobile'], -6, 6))//取手机号后6位为登录密码
        );
        //如果是婚宴，获取新娘信息
        if($post_data['venue_type'] == C('party.wedding.id')){
            $user_data[] = array(
                            'mobile_phone' => $post_data['roles_wife_mobile'],
                            'realname' => $post_data['roles_wife'],
                            'password' => get_encode_pwd(substr($post_data['roles_wife_mobile'], -6, 6))
            );
        }
        
        #创建新郎、新娘登录账号
        //判断账号表是否存在手机号
        foreach ($user_data as $k => $v){
            $user_exist = $this->Muser->get_one('id', array('mobile_phone' => $v['mobile_phone']));
            if($user_exist){
                $user_data[$k]['user_id'] = $user_exist['id'];
            }else{
                $user_data[$k]['user_id'] = $this->Muser->create($v);
            }
        }
        //关联用户账号id和宴会id
        $user_data[] = $customer_data;
        foreach ($user_data as $k => $v){
            $user_dinner[] = array(
                            'user_id' => $v['user_id'],
                            'dinner_id' => $dinner_id
            );
        }
        $this->Muser_dinner->create_batch($user_dinner);
        //to do 发送短信
        
        return $customer_id;
    }
    
    /**
     * 判断该天是否已被预约
     * 如果有，返回当天被预约的场馆
     * @author chaokai@gz-zc.cn
     */
    private function appoint_exist($venue_ids = array(), $date = ''){
        //返回变量，（id：0表示未有预约，1表示某个场馆该天有预约）
        //如果场馆被预约返回哪个场馆被预约，提示修改
        $return_arr = array(
                        'id' => 0,
                        'name' => ''
        );
        $dinner_list = $this->Mdinner_venue->get_lists('dinner_id,venue_id', array('in' => array('venue_id' => $venue_ids)));
        if(!$dinner_list){
            return $return_arr;
        }
        $dinner_ids = array_column($dinner_list, 'dinner_id');
        //查询场馆预约情况
        $appoint_list = $this->Mdinner->get_lists('id', array('in' => array('id' => $dinner_ids), 'solar_time' => $date, 'is_del' => 0));
        
        if(!$appoint_list){
            return $return_arr;
        }
        
        //获取被预约场馆的名称，提示更换场馆
        $venue_ids = array();
        foreach ($dinner_list as $k => $v){
            foreach ($appoint_list as $key => $value){
                if($value['id'] == $v['dinner_id']){
                    $venue_ids[] = $v['venue_id'];
                }
            }
        }
        
        $venue_list = $this->Mvenue->get_lists('id,name', array('in' => array('id' => $venue_ids)));
        $return_arr['id'] = 1;
        foreach ($venue_list as $k => $v){
            $return_arr['name'] .= $v['name'].';';
        }
        
        return $return_arr;
    }
    
    /**
     * 处理列表数据，如果该天没有预定，列表中显示为空
     * @author chaokai@gz-zc.cn
     */
    private function deal_list($list, $month, $year){
        $this->load->helper('date');
        
        $days = days_in_month($month, $year);
        if($month < 10){
            $month = '0'.$month;
        }
        
        for ($i = 1; $i <= $days; $i++){
            $temp = false;
            $day_text = $i >= 10 ? $i : '0'.$i;
            foreach ($list as $k => $v){
                if($v['solar_time'] == $year.'-'.$month.'-'.$day_text){
                    $temp = true;
                }
            }
            if($temp == false){
                $list[]['solar_time'] = $year.'-'.$month.'-'.$day_text;
            }
        }
        
        //重新按时间排序数组
        usort($list, function($a, $b){
            $a_time = strtotime($a['solar_time']);
            $b_time = strtotime($b['solar_time']);
            
            if($a_time == $b_time){
                return 0;
            }
            return $a_time > $b_time ? 1 : -1;
        });
        return $list;
    }
}
